package com.relsell.admin;

/**
 * DB classes would implmented in it.
 * application admin and admin user related
 *
 */

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.relsell.utility.AppVars;
import com.relsell.utility.UserPin;

public class DbHelper {

	static final String TABLE_PRODUCT_DATA = "product_data";
	static final String TABLE_MAP_DATA = "maps_data";
	static final String TABLE_USER = "user_table";

	public static final String FIELD_ID = "id";
	static final String FIELD_PRODUCT_TYPE = "product_type";

	public static final String FIELD_USER_ID = "userid";
	public static final String FIELD_SELLING_DATA = "selling_date";
	public static final String FIELD_LATITUDE = "latitude";
	public static final String FIELD_LONGITUDE = "longitude";
	public static final String FIELD_USERNAME = "username";
	public static final String FIELD_USERTYPE = "usertype";
	public static final String FIELD_PRODUCT_DATA = "product_data";
	public static final String FIELD_TARGET_ACHIEVED_DATA = "target_achieved_data";
	static final String FIELD_U_USERNAME = "username";
	static final String FIELD_U_USERIMAGE = "user_image";
	public static final String UPDATE_USER_DATA = "UPDATE " + TABLE_USER +" SET ";

	static final int DB_VERSION = 1;
	static final String DATABASE_NAME = "relsell";

	static final String QUERY_PRODUCT = "create table if not exists " + TABLE_PRODUCT_DATA
			+ "(" + FIELD_ID + " integer primary key autoincrement, "
			+ FIELD_PRODUCT_TYPE + " text not null);"; // , first_number double
														// not null,
														// second_number double
														// not null
	static final String QUERY_MAP = "create table if not exists " + TABLE_MAP_DATA + "("
			+ FIELD_ID + " integer primary key autoincrement, "
			+ FIELD_SELLING_DATA + " DATETIME DEFAULT CURRENT_DATE , "
			+ FIELD_USER_ID + " integer not null, " + FIELD_LATITUDE
			+ " double not null, " + FIELD_LONGITUDE + " double not null, "
			+ FIELD_TARGET_ACHIEVED_DATA + " text default nullvalue , "
			+ FIELD_USERNAME + " text not null, " + FIELD_PRODUCT_DATA
			+ " text not null);";
	static final String QUERY_USER = "create table if not exists " + TABLE_USER + "("
			+ FIELD_USER_ID + " integer, "
			+ FIELD_USERTYPE + " integer, "
			+ FIELD_U_USERNAME + " text not null);";

	static SQLiteDatabase database;
	Context context;
	static Helper helper;

	public DbHelper(Context c) {
		this.context = c;
		helper = new Helper(c);
	}

	static class Helper extends SQLiteOpenHelper {
		Helper(Context c) {
			super(c, DATABASE_NAME, null, DB_VERSION);
			if(AppVars.isLogEnabled) {
				Log.d(AppVars.LOG_TAG,"ab context");
			}
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			try {
				if(AppVars.isLogEnabled) {
					Log.d(AppVars.LOG_TAG,"inside oncreate");
				}
				if(AppVars.isLogEnabled) {
					Log.i(AppVars.LOG_TAG,"query "+QUERY_PRODUCT);
				}
				if(AppVars.isLogEnabled) {
					Log.i(AppVars.LOG_TAG,"query "+QUERY_MAP);
				}
				db.execSQL(QUERY_PRODUCT);
				if(AppVars.isLogEnabled) {
					Log.i(AppVars.LOG_TAG,"cp"+"1");
				}
				db.execSQL(QUERY_MAP);
				db.execSQL(QUERY_USER);
			} catch (Exception e) {
				Log.d("checkpoint ", "1");
				e.printStackTrace();
			}

		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCT_DATA + "");
			db.execSQL("DROP TABLE IF EXISTS " + TABLE_MAP_DATA + "");
			db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER + "");
			onCreate(db);
		}
	}

	public DbHelper open() throws SQLException {
		Log.d("cp", "open method");
		database = helper.getWritableDatabase();
		return this;
	}

	public void close() {
		Log.d("cp", "close method");
		helper.close();
	}

	public long insert_product_data(String product) {
		ContentValues cov = new ContentValues();
		Log.i("db", "3");
		cov.put(FIELD_PRODUCT_TYPE, product);
		Log.i("db", "4");
		return database.insert(TABLE_PRODUCT_DATA, null, cov);
	}

	public void deleteMapDataForDate(String date) {
		database.execSQL("DELETE FROM "+TABLE_MAP_DATA+" WHERE selling_date='"+date+"'");
	}
	public long insert_map_data(String date, String userid, String lat,
			String lon, String username, String product,String tdata) {
		
		Double latitude = Double.parseDouble(lat);
		Log.i("db", "1");
		Double longitude = Double.parseDouble(lon);
		Log.i("db", "2");
		ContentValues cov = new ContentValues();
		cov.put(FIELD_SELLING_DATA, date);
		cov.put(FIELD_USER_ID, userid);
		cov.put(FIELD_LATITUDE, latitude);
		cov.put(FIELD_LONGITUDE, longitude);
		cov.put(FIELD_TARGET_ACHIEVED_DATA, tdata);
		cov.put(FIELD_USERNAME, username);
		cov.put(FIELD_PRODUCT_DATA, product);
		return database.insert(TABLE_MAP_DATA, null, cov);
	}

	public List<String> read_product_data() {
		List<String> labels = new ArrayList<String>();
		String selectQuery = "SELECT  * FROM " + TABLE_PRODUCT_DATA;
		Cursor cursor = database.rawQuery(selectQuery, null);
		if (cursor.moveToFirst()) {
			do {
				labels.add(cursor.getString(1));
			} while (cursor.moveToNext());
		}
		cursor.close();
		return labels;
	}

	public Cursor read_map_data() {
		String map[] = { FIELD_ID,FIELD_TARGET_ACHIEVED_DATA,FIELD_SELLING_DATA, FIELD_USER_ID, FIELD_LATITUDE,
				FIELD_LONGITUDE, FIELD_USERNAME, FIELD_PRODUCT_DATA };
		Log.d("Checkp point", "db5");

		Log.d("Checkp point", "db7");
		return database
				.query(TABLE_MAP_DATA, map, null, null, null, null, null);

	}
	public long insertInUserTable(String userId,String userName,String userType) {
		ContentValues cov = new ContentValues();
		Log.i("db", "3");
		cov.put(FIELD_USER_ID,userId);
		cov.put(FIELD_USERTYPE,userType);
		cov.put(FIELD_USERNAME,userName);
		return database.insert(TABLE_USER, null, cov);
	}
	public Cursor readUserTableData() {
		String map[] = { FIELD_USER_ID,FIELD_U_USERNAME,FIELD_USERTYPE };

		return database
				.query(TABLE_USER, map, null, null, null, null, null);

	}
	public int updateUserTableData(String userId,String userName,String userType) {
		ContentValues cov = new ContentValues();
		if (AppVars.isLogEnabled) {
			Log.v(AppVars.LOG_TAG,"Inside update UserTable");
		}
		cov.put(FIELD_USER_ID,userId);
		cov.put(FIELD_USERTYPE,userType);
		cov.put(FIELD_USERNAME,userName);
		int res = database.update(TABLE_USER, cov,FIELD_USER_ID+"=?",new String[]{userId}); 
		if (AppVars.isLogEnabled) {
			Log.v(AppVars.LOG_TAG,"Result ="+res);
		}
		return res;
	}
}
